<!-- data -->
<div class="panel panel-default" style="margin-top: 20px">
    <div class="panel-body">
        <table id="table" data-toggle="table" data-unique-id="id" data-show-columns="true" data-show-refresh="true"
            data-show-toggle="true" data-search="true" data-search-on-enter-key="true" data-pagination="true"
            data-side-pagination="server" data-page-list="[10, 25, 50, 100]" data-total-field="totalElements"
            data-data-field="content" data-url="/api/jobHistory" data-query-params="queryParams" data-data-type="text"
            data-response-handler="responseHandler">
        </table>
    </div>
</div>
<script>
    operateEvents = {
        'click .remove': function (e, value, row, index) {
            const id = row.id
            Swal.fire({
                title: '警告',
                text: "删除之后无法恢复",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                cancelButtonColor: '#3085d6',
                confirmButtonText: '坚持删除',
                cancelButtonText: '取消'
            }).then((result) => {
                if (result.value) {
                    $.ajax({
                        type: "DELETE",
                        url: "/api/jobHistory/" + id,
                        dataType: "json",
                        success: function (result) {
                            if (result.success) {
                                toastr.success('删除成功')
                                $('#table').bootstrapTable('refresh')
                            } else {
                                toastr.error(result.message)
                            }
                        }
                    })
                }
            })
        }
    }

    $('#table').bootstrapTable({
        locale: 'zh-CN',
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'jobId',
            title: '任务ID'
        }, {
            field: 'statusMsg',
            title: '任务状态'
        }, {
            field: 'createAt',
            title: '创建时间'
        }, {
            field: 'updateAt',
            title: '更新时间'
        }, {
            field: 'logText',
            title: '日志'
        }, {
            field: 'operate',
            title: '操作',
            events: operateEvents,
            formatter: operateFormatter
        }],
    })

    function queryParams(params) {
        if (params.search) {
            params.jobId = params.search
        }
        params.page = params.offset / params.limit
        params.size = params.limit
        params.sort = 'createAt,desc'
        delete params.search
        delete params.offset
        delete params.limit
        delete params.order
        return params
    }

    function responseHandler(result) {
        const resp = JSON.parse(result)
        if (resp.success) {
            const data = resp.data
            return data
        }
        return { content: [], totalElements: 0 }
    }

    function operateFormatter(value, row, index) {
        return [
            '<a class="remove" title="Remove">',
            '<i class="fa fa-trash"></i>',
            '</a>'
        ].join('')
    }
</script>